package com.apes.fn.scm.saleReport.service.process.orderProcess;

import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.fn.crm.customerExpand.FnCustomerExpandRepository;
import com.apes.fn.scm.retailReturn.RetailReturnItemRepository;
import com.apes.fn.scm.saleReport.service.process.CreateDetailsProcess;
import com.apes.fn.server.productReceive.ProductReceiveItem;
import com.apes.fn.server.productReceive.ProductReceiveItemRepository;
import com.apes.fn.server.servicePlan.model.ServicePlanItem;
import com.apes.fn.server.servicePlan.repository.ServicePlanItemRepository;
import com.apes.fn.server.workOrder.model.MaintenanceOrder;
import com.apes.fn.server.workOrder.repository.MaintenanceOrderRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarLabel;
import com.apes.scm.masterdata.car.model.CarModel;
import com.apes.scm.masterdata.car.repository.CarConfigurationRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.sal.saleOrder.SaleOrder;
import com.apes.scm.sal.saleOrder.SaleOrderItem;
import com.apes.scm.sal.saleOrder.SaleOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 分销单生成营业额明细
 */
@Service
public class SaleOrderProcess extends CreateDetailsProcess {
    @Autowired
    private SaleOrderRepository saleOrderRepository;
    @Autowired
    private CarConfigurationRepository carConfigurationRepository;
    @Autowired
    private FnCustomerExpandRepository fnCustomerExpandRepository;
    @Autowired
    private ServicePlanItemRepository servicePlanItemRepository;
    @Autowired
    private MaintenanceOrderRepository maintenanceOrderRepository;
    @Autowired
    private ProductReceiveItemRepository productReceiveItemRepository;

    @Override
    public void process(SimpleRequest request) {
        String id = request.get("id");
        SaleOrder saleOrder = saleOrderRepository.findOne(id);
        saleOrder.getSaleItems().forEach(saleOrderItem -> createDetails(MapUtil.mapper("order", saleOrderItem)));
    }

    /**
     * @param map {"order": SaleOrderItem}
     *
     */
    @Override
    public Map<String, Object> getModel(Map map) {
        SaleOrderItem saleOrderItem = (SaleOrderItem) map.get("order");
        SaleOrder saleOrder = saleOrderItem.getSaleOrder();
        Product product = saleOrderItem.getProduct();
        String fnCustomerExpandId = saleOrder.getFnCustomerExpandId();
        //厂牌
        CarLabel carLabel = null;
        //车型
        CarModel carModel = null;
        //车牌
        String carLicense = "";
        if(fnCustomerExpandId != null){
            //车辆扩展信息
            FnCustomerExpand fnCustomerExpand = fnCustomerExpandRepository.findOne(fnCustomerExpandId);
            carLicense = fnCustomerExpand.getCarLicense();
            String jekunModelCode = fnCustomerExpand.getJekunModelCode();
            if(jekunModelCode != null) {
                //配置车型
                CarConfiguration carConfiguration = carConfigurationRepository.findOne(jekunModelCode);
                if(carConfiguration != null){
                    carLabel = carConfiguration.getCarLabel();
                    carModel = carConfiguration.getCarModel();
                }
            }
        }
        ServicePlanItem servicePlanItem = null;
        MaintenanceOrder maintenanceOrder = null;
        PartyRole receiver = null;
        String worker = "";
        //通过来源行号找方案明细
        if(saleOrderItem.getOriginItemId() != null){
            servicePlanItem = servicePlanItemRepository.findOne(saleOrderItem.getOriginItemId());
            if(servicePlanItem != null){
                //如果方案明细有关联工单号
                if (servicePlanItem.getRelevanceWorkOrder() != null) {
                    maintenanceOrder = maintenanceOrderRepository.findOne(servicePlanItem.getRelevanceWorkOrder());
                } else {
                    //没有关联工单号就通过方案明细id找工单
                    maintenanceOrder = maintenanceOrderRepository.findByServicePlanItemAndBusinessType(servicePlanItem,"common");
                }
                //根据方案明细找到最后一张领用单获取到领用人
                ProductReceiveItem productReceiveItem = productReceiveItemRepository.findMaxByServicePlanItemId(servicePlanItem.getId());
                if(productReceiveItem != null) receiver = productReceiveItem.getProductReceive().getReceiver();
            }
        }
        PartyRole mainSalesman = null;
        PartyRole subSalesman = null;
        if(servicePlanItem != null){
            mainSalesman = servicePlanItem.getMainSaler();
            subSalesman = servicePlanItem.getSupSaler();
        }
        if(maintenanceOrder != null) worker = maintenanceOrder.getWorker1() + "|" +maintenanceOrder.getWorker2() + "|" + maintenanceOrder.getWorker2() + "|" + maintenanceOrder.getWorker2();
        return MapUtil.mapper("carLicense",carLicense,"product",product,
                "productUom",saleOrderItem.getUom(),"qty",saleOrderItem.getProductUomQty(),"salesAmount",saleOrderItem.getPriceTotal(),
                "serveType",product.getServeType(),"dept",saleOrder.getDept(),"company",saleOrder.getCompany(),
                "origin",saleOrder.getId(),"originItem",saleOrderItem.getId(),"originBusinessType","saleOrder",
                "originCreateDate",saleOrder.getDateApprove(),"source", saleOrder.getOrigin(),"sourceBusinessType",saleOrder.getOriginBusinessType(),
                "productGroup",product.getProductGroup(),"classify",product.getClassify(),
                "brand",product.getBrand(),"tax",saleOrderItem.getTax(),"carLabel",carLabel,"carModel",carModel,
                "pftPriceUnit",saleOrderItem.getPriceUnit(),"priceUnit",saleOrderItem.getPriceUnit(),
                "discountAmount",saleOrderItem.getDiscount(),"couponDiscountAmount",0.00,"receiver",receiver,
                "mainSalesman",mainSalesman,"subSalesman",subSalesman,"worker",worker);
    }

    @Override
    public boolean support(String type) {
        return "saleOrder".equals(type);
    }

}
